package com.bdk.weixin;



import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * HttpGetRequest用于连接验证服务器
 * @author Administrator
 *
 */
public class HttpGetRequest {  
	  
	public static void main(String[] args) {
		String content;
		try {
			content = doGet("http://www.baidu.com/");
			System.out.println(content);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
    /** 
     * Get Request 
     *  
     * @return 
     * @throws Exception 
     */  
    public static String doGet(String url) throws Exception {  
        URL localURL = new URL(url);  
        URLConnection connection = localURL.openConnection();  
        HttpURLConnection httpURLConnection = (HttpURLConnection) connection;  
        httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");  
        httpURLConnection.setRequestProperty("Content-Type",  
                "application/text");  
  
        InputStream inputStream = null;  
        InputStreamReader inputStreamReader = null;  
        BufferedReader reader = null;  
        StringBuffer resultBuffer = new StringBuffer();  
        String tempLine = null;  
  
        if (httpURLConnection.getResponseCode() >= 300) {  
            throw new Exception(  
                    "HTTP Request is not success, Response code is "  
                            + httpURLConnection.getResponseCode());  
        }  
  
        try {  
            inputStream = httpURLConnection.getInputStream();  
            inputStreamReader = new InputStreamReader(inputStream);  
            reader = new BufferedReader(inputStreamReader);  
  
            while ((tempLine = reader.readLine()) != null) {  
                resultBuffer.append(tempLine);  
            }  
  
        } finally {  
  
            if (reader != null) {  
                reader.close();  
            }  
  
            if (inputStreamReader != null) {  
                inputStreamReader.close();  
            }  
  
            if (inputStream != null) {  
                inputStream.close();  
            }  
  
        }  
        return resultBuffer.toString();  
    }  
} 